using System.Data.SqlClient;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using DataStubGenerator;
using DataStubGenerator.Exceptions;
using Lib.Properties;

namespace DataStubGenerator
{
    public class Logger
    {
        private static Logger _instance;

        public static Logger Instance
        {
            get
            {
                if(_instance==null) _instance=new Logger();
                return _instance;
            }
        }

        protected Logger(){}

        public void Log(string message, string title)
        {
            //ND: todo
            //...
        }

        public  void LogAndDisplay(string message, string title)
        {
            Log(message,title);
            MessageBox.Show(message, title);
        }

        public void LogException(ThreadExceptionEventArgs e)
        {
            if(e.Exception is ILoggable)
            {
                ILoggable logable = e.Exception as ILoggable;
                if (logable != null)
                    if(logable.ShouldDisplay)
                        LogAndDisplay(e.Exception.Message, logable.Caption);
                    else
                        Log(e.Exception.Message, logable.Caption);
            }
            else if (e.Exception is SqlException)
                LogAndDisplay(e.Exception.Message, Resources.SqlError);
            else if (e.Exception is XmlException)
                LogAndDisplay(e.Exception.Message, Resources.XmlError);
            else
                LogAndDisplay(e.Exception.Message, Resources.UnexpectedException);
        }

    }
}
